package com.test.nnooolwdeaer.activity_fragment_wooden.fragment_wooden;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseLoanFragment_wooden;
import com.test.nnooolwdeaer.bean_wooden.config_info_wooden.AppShowInfo_wooden;
import com.test.nnooolwdeaer.bean_wooden.process_wooden.ProcessInfo_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.dialog_wooden.CopyOrderNumberDialog_wooden;
import com.test.nnooolwdeaer.util_wooden.ThreadUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.helper_wooden.UserInfoHelper_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class SignContractFragment extends BaseLoanFragment_wooden {

    private TextView orderNumTv;
    private ImageButton copyOrderNumIb;
    private ImageView appLogoIv;
    private TextView appNameTv,devNameTv;
    private TextView appScoreTv,appAgeLimitTv,appDownloadsTv;
    private View btnView;
    private TextView btnTv;

    private String appUrl;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sign_contract, container, false);
        init(view);
        return view;
    }

    @SuppressLint("SetTextI18n")
    private void init(View view){

        orderNumTv = view.findViewById(R.id.fr_sign_tv_order_number);
        copyOrderNumIb = view.findViewById(R.id.fr_sign_ib_copy_order_number);
        appLogoIv = view.findViewById(R.id.fr_sign_iv_app_logo);
        appNameTv = view.findViewById(R.id.fr_sign_tv_app_name);
        devNameTv = view.findViewById(R.id.fr_sign_tv_dev_name);
        appScoreTv = view.findViewById(R.id.fr_sign_tv_app_score);
        appAgeLimitTv = view.findViewById(R.id.fr_sign_tv_app_age_limit);
        appDownloadsTv = view.findViewById(R.id.fr_sign_tv_app_downloads);
        btnView = view.findViewById(R.id.fr_sign_view_btn);
        btnTv = view.findViewById(R.id.fr_sign_tv_btn);

        String orderId = ProcessInfo_wooden.getInstance_wooden().getSignOrderId();

        appUrl = AppShowInfo_wooden.getInstance().getSignScheme() +"://"+AppShowInfo_wooden.getInstance().getSignHost()+"?"+
                Constant_wooden.Key_OrderId+"="+ orderId+"&"+
                Constant_wooden.Key_Mobile+"="+ UserInfoHelper_wooden.getInstance_wooden().getMobile_wooden() +"&"+
                Constant_wooden.Key_Product+"="+ Constant_wooden.PRODUCT_wooden+"&"+
                Constant_wooden.Key_SourceType+"="+ getString(R.string.app_name)+"&"+
                Constant_wooden.Key_Scheme+"="+ Constant_wooden.Scheme+"&"+
                Constant_wooden.Key_Host+"="+ Constant_wooden.Host;

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(appUrl));
        Activity activity = getActivity();
        Log.w("---appurl---", appUrl );
        //签约app已存在
        if (activity!=null && intent.resolveActivity(activity.getPackageManager())!=null){
            btnTv.setText(getString(R.string.sign_jump_to)+" "+AppShowInfo_wooden.getInstance().getSignAppName());
            btnView.setOnClickListener(v -> {
                HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint((pageTag_wooden+"_JUMP_TO_SIGN_APP_CLICK").toUpperCase());
                startActivity(intent);
            });
        }
        //签约app不存在
        else {
            btnTv.setText(getString(R.string.sign_install_btn));
            btnView.setOnClickListener(v -> {
                HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint((pageTag_wooden+"_JUMP_TO_SPLAY_STORE_CLICK").toUpperCase());
                startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(AppShowInfo_wooden.getInstance().getSignUrl())));
            });
        }

        orderNumTv.setText(orderId);
        copyOrderNumIb.setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint((pageTag_wooden+"_COPY_ORDER_NUMBER_CLICK").toUpperCase());
            new CopyOrderNumberDialog_wooden(requireContext()).show();
        });

        String logoUrl = AppShowInfo_wooden.getInstance().getSignLogo();
        if (!TextUtils.isEmpty(logoUrl)){
            ThreadUtil_wooden.getInstance_wooden().runOnChildThread_wooden(() -> {
                try {
                    URL url = new URL(logoUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        ThreadUtil_wooden.getInstance_wooden().runOnUiThread_wooden(() -> {
                            appLogoIv.setImageBitmap(bitmap);
                        });
                    }
                } catch (IOException e) {
                    if (Constant_wooden.Debug) e.printStackTrace();
                }

            });
        }

        appNameTv.setText(AppShowInfo_wooden.getInstance().getSignAppName());
        devNameTv.setText(AppShowInfo_wooden.getInstance().getSignDevName());

        appScoreTv.setText(AppShowInfo_wooden.getInstance().getSignScore());
        appAgeLimitTv.setText(AppShowInfo_wooden.getInstance().getSignAge());
        appDownloadsTv.setText(AppShowInfo_wooden.getInstance().getSignDownloads());

    }


}
